package search

import (
	"fmt"
	"io"
	"net/http"
	"os"
	"path"
)

const assetDir = "assets/"

func download(url string) {
	res, err := http.Get(url)
	if err != nil {
		panic(err)

	}
	filename := assetDir + path.Base(url)
	if IsExist(filename) {
		return
	}
	f, err := os.Create(assetDir + path.Base(url))
	if err != nil {
		return
	}
	io.Copy(f, res.Body)
}

func downloadAll() {
	fmt.Println("starting download")
	for _, img := range imgMap {
		download(img)
	}
}

func IsExist(path string) bool {
	_, err := os.Stat(path)
	return err == nil || os.IsExist(err)
}
